package com.sensetime.jv.controller;


import com.sensetime.jv.annotation.RequirePermission;
import com.sensetime.jv.entity.ReturnWrapper;
import com.sensetime.jv.service.impl.AuthServiceImpl;
import com.sensetime.jv.util.UserInfoUtils;
import com.sensetime.jv.vo.request.LoginReqVO;
import com.sensetime.jv.vo.response.LoginRespVO;
import com.sensetime.jv.vo.response.LoginUserInfoRespVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user") // 添加基础路径
@Slf4j
public class AuthController {


    @Resource
    private AuthServiceImpl authService;

    @PostMapping("login")
    @RequirePermission(description = "登录接口",whiteUrl = true)
    public ReturnWrapper<LoginRespVO> login(@RequestBody LoginReqVO loginReqVO){
        return ReturnWrapper.success(authService.login(loginReqVO));
    }

    @PostMapping("logout")
    @RequirePermission(description = "退出接口")
    public ReturnWrapper<Void> logout(){
        authService.logout();
        return ReturnWrapper.success();
    }

    @PostMapping("info")
    @RequirePermission(description = "查询用户信息")
    public ReturnWrapper<LoginUserInfoRespVO> info(){
        LoginUserInfoRespVO respVO = new LoginUserInfoRespVO();
        respVO.setUserName(UserInfoUtils.getUserInfo().getUser().getUsername());
        respVO.setPermissions(UserInfoUtils.getUserInfo().getPermissions());
        respVO.setRoles(UserInfoUtils.getUserInfo().getRoles());
        return ReturnWrapper.success(respVO);
    }
}
